package algotithm.leetcode.dp.test978;

/**
 * @author zhouyanxiang
 * @Date 2021-01-2021/1/12-10:14
 */
public class Solution {

    public static void main(String[] args) {
        int[] arr = {9,4,2,10,7,8,8,1,9};
        Solution solution = new Solution();
        int size = solution.maxTurbulenceSize(arr);
        System.out.println(size);
    }

    public int maxTurbulenceSize(int[] arr) {
        if (arr.length <= 1) {
            return arr.length;
        }
        int max = 1 , kind = 0, curLength = 1;
        for (int i = 0; i < arr.length - 1; i++) {
            int diff = arr[i] - arr[i+1];
            if (diff == 0) {
                max = Math.max(max,curLength);
                curLength = 1;
            } else {
                diff = diff > 0 ? 1 : -1;
                if (diff != kind) {
                    curLength++;
                } else {
                    max = Math.max(max,curLength);
                    curLength = 2;
                }
            }
            kind = diff;
        }
        return max;
    }



}
